package org.sun.sunshine.io.nio.bufferDemo;


import lombok.extern.slf4j.Slf4j;

import java.nio.IntBuffer;

@Slf4j
public class UseBuffer
{
    static IntBuffer intBuffer = null;
    public static void main(String[] args)
    {
        mark_reset();
    }



    public static void mark_reset(){
        intBuffer = IntBuffer.allocate(20);
        for(int i=0;i<20;i++){
            intBuffer.put(i);
        }
        intBuffer.flip();
        intBuffer.get();
        log.info("position {}",intBuffer.position());
        log.info("limit {}",intBuffer.limit());
        //记录position 到mark
        intBuffer.mark();
        log.info("position {}",intBuffer.position());
        intBuffer.get();
        log.info("position {}",intBuffer.position());
        //将mark 的值reset 到position
        intBuffer.reset();
        log.info("position {}",intBuffer.position());
        log.info("mark {}",intBuffer.mark());
    }




    public static void basic2(){
        intBuffer = IntBuffer.allocate(20);
        for(int i=0;i<20;i++){
            intBuffer.put(i);
        }

        intBuffer.flip();
        //当前读写的位置
        log.info("position={}" , intBuffer.position());
        //读写的最大限制
        log.info("limit={}" , intBuffer.limit());
        //容量，创建时设置的
        log.info("capacity={}" , intBuffer.capacity());

        intBuffer.get();
        intBuffer.get();
        log.info("after get");
        //当前读写的位置
        log.info("position={}" , intBuffer.position());
        //读写的最大限制
        log.info("limit={}" , intBuffer.limit());
        //容量，创建时设置的
        log.info("capacity={}" , intBuffer.capacity());

        //重新读
        intBuffer.rewind();
        log.info("after rewind");
        //当前读写的位置
        log.info("position={}" , intBuffer.position());
        //读写的最大限制
        log.info("limit={}" , intBuffer.limit());
        //容量，创建时设置的
        log.info("capacity={}" , intBuffer.capacity());

    }


    public static void basic(){
        //调用allocate方法，而不是使用new
        intBuffer = IntBuffer.allocate(20);
        //输出buffer的主要属性值
        log.info("------------after allocate------------------");
        //当前读写的位置
        log.info("position={}" , intBuffer.position());
        //读写的最大限制
        log.info("limit={}" , intBuffer.limit());
        //容量，创建时设置的
        log.info("capacity={}" , intBuffer.capacity());

        intBuffer.put(new int[]{1,2,3});
        log.info("------------after put------------------");
        log.info("position={}" , intBuffer.position());
        log.info("limit={}" , intBuffer.limit());
        log.info("capacity={}" , intBuffer.capacity());

        //转为读模式
        intBuffer.flip();
        log.info("------------after flip------------------");
        log.info("position={}" , intBuffer.position());
        log.info("limit={}" , intBuffer.limit());
        log.info("capacity={}" , intBuffer.capacity());

        int dest[]=new int[intBuffer.limit()];
        intBuffer.get(dest,0,intBuffer.limit());
        log.info("dest = {}",dest);

        //再转为写模式
        intBuffer.clear();
        //intBuffer.compact();
        intBuffer.put(new int[]{4,5,6});
        intBuffer.flip();
        log.info("read2 {}",intBuffer.get());
        log.info("read2 {}",intBuffer.get());
    }



}
